﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TimerTask.Common.Utils;
using TimerTask.Dal;
using TimerTask.Model.Common;
using TimerTask.Model.Entity;
using TimerTask.Model.Vms;
using TimerTask.Service;

namespace TimerTask.Bll
{
    public class JobBll
    {
        /// <summary>
        /// 获取分页信息
        /// </summary>
        /// <param name="index"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public Pager<JobInfoVm> GetPagerJob(int index, int groupId)
        {
            List<JobInfoVm> list = new JobInfoDal().GetJobInfoVms(index, groupId);
            int count = new JobInfoDal().GetJobInfoCount(groupId);
            return new Pager<JobInfoVm>(count, AppConfig.Instance.PageSize, index, list);
        }

        /// <summary>
        /// 获取所有组
        /// author:肖阳
        /// 2016-10-23 00:23:23
        /// </summary>
        /// <returns></returns>
        public List<GroupInfo> GetGroupInfos()
        {
            return new GroupInfoDal().GetGroupInfos();
        }

        /// <summary>
        /// 保存新建Jobinfo
        /// author:肖阳
        /// 2016-10-23 16:53:53
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public MsgResult<int> NewSave(JobInfo model)
        {
            model.JobStatus = JobStatus.Normal;
            model.LastFireTime = DateTime.MinValue;
            int addResult = new JobInfoDal().AddJobInfo(model);
            if (addResult == 1)
            {
                QuartzService.AddJob(model);


                return MsgResult<int>.Success(1);
            }
            return MsgResult<int>.Error("添加失败");
        }

        /// <summary>
        /// 开启任务
        /// author:肖阳
        /// date:2016-10-22 13:55:32
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public MsgResult<int> StartJob(int jobId)
        {
            JobInfoDal dal = new JobInfoDal();
            JobInfo jobInfo = dal.Query(jobId);
            if (jobInfo == null)
            {
                return MsgResult<int>.Error("找不到任务");
            }
            if (jobInfo.JobStatus == JobStatus.Del)
            {
                return MsgResult<int>.Error("任务已删除，不能启动！");
            }
            jobInfo.JobStatus = JobStatus.Normal;
            int i = dal.UpdateStatus(jobInfo);
            if (i > 0)
            {
                QuartzService.AddJob(jobInfo);
                return MsgResult<int>.Success(1);
            }
            return MsgResult<int>.Error("更新状态失败");
        }

        /// <summary>
        /// 更新任务为暂停
        /// author:肖阳
        /// date:2016-10-22 13:55:32
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public MsgResult<int> StopJob(int jobId)
        {
            JobInfoDal dal = new JobInfoDal();
            JobInfo jobInfo = dal.Query(jobId);
            if (jobInfo == null)
            {
                return MsgResult<int>.Error("找不到任务");
            }
            if (jobInfo.JobStatus == JobStatus.Del)
            {
                return MsgResult<int>.Error("任务已删除，不能停止！");
            }
            jobInfo.JobStatus = JobStatus.Stop;
            jobInfo.UpdateTime = DateTime.Now;
            int i = dal.UpdateStatus(jobInfo);
            if (i > 0)
            {
                QuartzService.Stop(jobInfo);
                return MsgResult<int>.Success(1);
            }
            return MsgResult<int>.Error("更新状态失败");
        }

        /// <summary>
        /// 单次触发任务
        /// author:肖阳
        /// date:2016-10-22 13:55:32
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public MsgResult<int> TriggerJob(int jobId)
        {
            JobInfoDal dal = new JobInfoDal();
            JobInfo jobInfo = dal.Query(jobId);
            if (jobInfo == null)
            {
                return MsgResult<int>.Error("找不到任务");
            }
            if (jobInfo.JobStatus == JobStatus.Del)
            {
                return MsgResult<int>.Error("任务已删除，不能开启！");
            }
            QuartzService.Trigger(jobInfo);

            return MsgResult<int>.Success(1);
        }

        /// <summary>
        /// 获取计数信息
        /// author:肖阳
        /// date:2016-10-22 13:55:32
        /// </summary>
        /// <returns></returns>
        public Tuple<string, string, string> GetCounts()
        {
            return new JobInfoDal().GetJobCounts();
        }


        public List<Tuple<string, string, DateTime>> GetFailLog()
        {
            return new TriggerLogDal().GetFailLog();
        }
    }
}
